package com.webmining.sherlock.util;

import com.webmining.sherlock.dto.Role;
import com.webmining.sherlock.dto.User;

import java.util.HashMap;
import java.util.Map;

public class UserContainer {

    private Map<String, User> users = new HashMap<String, User>();

    private static UserContainer container = null;

    {
        users.put("admin", new User("admin", DataEncryptor.encrypt("sherlock_admin"), Role.ADMIN));
    }

    private UserContainer(){}

    public static synchronized UserContainer getInstance(){
        if(container == null){
            container = new UserContainer();
        }
        return container;
    }

    public boolean userExists(String login) {
        return users.containsKey(login);
    }

    public void addUser(String login, String password){
        users.put(login, new User(login, password, Role.USER));
    }

    public String getUserPassword(String login){
        if (login == null)
            throw new IllegalArgumentException("login must be not null");
        return users.get(login).getPassword();
    }

    public Role getUserRole(String login){
        if (login == null)
            throw new IllegalArgumentException("login must be not null");
        return users.get(login).getRole();
    }
}
